package org.qiyu.live.api.service.impl;

import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.live.api.service.IBankService;
import org.qiyu.live.api.vo.req.PayProductReqVO;
import org.qiyu.live.api.vo.resp.PayProductItemVO;
import org.qiyu.live.api.vo.resp.PayProductRespVO;
import org.qiyu.live.api.vo.resp.PayProductVO;
import org.qiyu.live.bank.constants.PayOrderStatusEnum;
import org.qiyu.live.bank.dto.PayOrderDTO;
import org.qiyu.live.bank.dto.PayProductDTO;
import org.qiyu.live.bank.interfaces.IPayOrderRpc;
import org.qiyu.live.bank.interfaces.IPayProductRpc;
import org.qiyu.live.bank.interfaces.IQiyuCurrencyAccountRpc;
import org.qiyu.live.web.starter.context.QiyuRequestContext;
import org.qiyu.live.web.starter.error.ErrorAssert;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.qiyu.live.web.starter.error.QiYuBaseErrorEnum.PARAM_ERROR;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-11-04
 * Time: 23:04
 */
@Service
public class BankServiceImpl implements IBankService {

    @DubboReference
    private IPayProductRpc payProductRpc;

    @DubboReference
    private IQiyuCurrencyAccountRpc qiyuCurrencyAccountRpc;

    @DubboReference
    private IPayOrderRpc payOrderRpc;

    @Override
    public PayProductVO products(Integer type) {
        List<PayProductDTO> products = payProductRpc.products(type);
        List<PayProductItemVO> payProductItemVOList = new ArrayList<>();
        PayProductVO payProductVO = new PayProductVO();
        for (PayProductDTO product : products) {
            PayProductItemVO itemVO = new PayProductItemVO();
            itemVO.setId(product.getId());
            itemVO.setName(product.getName());
            itemVO.setCoinNum(JSON.parseObject(product.getExtra()).getInteger("coin"));
            payProductItemVOList.add(itemVO);
        }
        payProductVO.setPayProductItemVOList(payProductItemVOList);
        payProductVO.setCurrentBalance(qiyuCurrencyAccountRpc.getBalance(QiyuRequestContext.getUserId()));
        return payProductVO;
    }

    @Override
    public PayProductRespVO payProduct(PayProductReqVO payProductReqVO) {
        // 1. 对参数进行校验
        ErrorAssert.isNotBlank(payProductReqVO!=null && payProductReqVO.getProductId()!=null &&
                payProductReqVO.getPayChannel()!=null, PARAM_ERROR);

        ErrorAssert.isNotNull(payProductReqVO.getPaySource(),PARAM_ERROR);


        // 2. 根据productId查询出payProductDTO
        PayProductDTO payProductDTO = payProductRpc.getPayProductById(payProductReqVO.getProductId());
        ErrorAssert.isNotNull(payProductDTO,PARAM_ERROR);

        // 3. 生成一个支付订单(待支付状态),插入订单
        PayOrderDTO payOrderDTO = new PayOrderDTO();
        payOrderDTO.setUserId(QiyuRequestContext.getUserId());
        payOrderDTO.setStatus(PayOrderStatusEnum.UNPAID.getCode());
        payOrderDTO.setPayTime(new Date());
        payOrderDTO.setProductId(payProductReqVO.getProductId());
        payOrderDTO.setSource(payOrderDTO.getSource());
        payOrderDTO.setPayChannel(payOrderDTO.getPayChannel());
        String orderId = payOrderRpc.insertOne(payOrderDTO);
        ErrorAssert.isNotNull(orderId,PARAM_ERROR);

        //.....................此时假设进入支付页面,将订单改为支付中状态
        // 4 修改订单状态为支付中状态
        payOrderRpc.updateOrderStatus(orderId,PayOrderStatusEnum.DURING_PAY.getCode());


        // 当第三方支付完成之后,会触发一个回调给到bank-api
        // 5. 会发送一个回调消息给到bank-api服务
        // 发送一个http请求给到bank-api服务



        // 6. 返回
        PayProductRespVO payProductRespVO = new PayProductRespVO();
        payProductRespVO.setOrderId(orderId);
        return payProductRespVO;
    }
}